package com.lineage.server.model;

import com.lineage.server.datatables.ExtraAttrWeaponTable;
import com.lineage.server.model.Instance.L1DollInstance;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_CurseBlind;
import com.lineage.server.serverpackets.S_Paralysis;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1AttrWeapon;
import com.lineage.server.utils.L1SpawnUtil;
import com.lineage.server.world.World;
import java.util.Iterator;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1AttackPower {
  private static final Log _log = LogFactory.getLog(L1AttackPower.class);
  
  private static final Random _random = new Random();
  
  private final L1PcInstance _pc;
  
  private final L1Character _target;
  
  private L1PcInstance _targetPc;
  
  private L1NpcInstance _targetNpc;
  
  private int _weaponAttrEnchantKind;
  
  private int _weaponAttrEnchantLevel;
  
  public L1AttackPower(L1PcInstance attacker, L1Character target, int weaponAttrEnchantKind, int weaponAttrEnchantLevel) {
    this._pc = attacker;
    this._target = target;
    if (this._target instanceof L1NpcInstance) {
      this._targetNpc = (L1NpcInstance)this._target;
    } else if (this._target instanceof L1PcInstance) {
      this._targetPc = (L1PcInstance)this._target;
    } 
    this._weaponAttrEnchantKind = weaponAttrEnchantKind;
    this._weaponAttrEnchantLevel = weaponAttrEnchantLevel;
  }
  
  public void AttackPower1(int weaponAttrEnchantKind, int weaponAttrEnchantLevel) {
    this._weaponAttrEnchantKind = weaponAttrEnchantKind;
    this._weaponAttrEnchantLevel = weaponAttrEnchantLevel;
  }
  
  public int set_item_power(int damage) {
    int reset_dmg = damage;
    try {
      if (this._weaponAttrEnchantKind > 0) {
        L1AttrWeapon attrWeapon = ExtraAttrWeaponTable.getInstance().get(this._weaponAttrEnchantKind, 
            this._weaponAttrEnchantLevel);
        if (attrWeapon == null)
          return damage; 
        int skillfor3 = this._pc.getlogpcpower_SkillFor3() * 10;
        if (attrWeapon.getProbability() + skillfor3 >= _random.nextInt(1000) + 1) {
          if (this._targetPc != null && this._targetPc.getInventory().consumeItem(44073, 1L)) {
            this._targetPc.sendPackets((ServerBasePacket)new S_SystemMessage("成功抵抗属性能力的发动"));
            return damage;
          } 
          if (attrWeapon.getTypeBind() > 0.0D && !L1WeaponSkill.isFreeze(this._target)) {
            int time = (int)(attrWeapon.getTypeBind() * 1000.0D);
            this._target.broadcastPacketAll((ServerBasePacket)new S_SkillSound(this._target.getId(), attrWeapon.getgfixd()));
            if (this._targetPc != null) {
              this._targetPc.setSkillEffect(4000, time);
              this._targetPc.sendPackets((ServerBasePacket)new S_SkillSound(this._target.getId(), attrWeapon.getgfixd()));
              this._targetPc.sendPackets((ServerBasePacket)new S_Paralysis(6, true));
            } else if (this._targetNpc != null) {
              this._targetNpc.setSkillEffect(4000, time);
              this._targetNpc.setPassispeed(0);
            } 
          } 
          if (attrWeapon.getTypeDrainHp() > 0.0D) {
            int drainHp = 1 + _random.nextInt((int)attrWeapon.getTypeDrainHp());
            S_SkillSound s_SkillSound = new S_SkillSound(this._target.getId(), attrWeapon.getgfixd());
            this._target.broadcastPacketX8((ServerBasePacket)s_SkillSound);
            this._pc.setCurrentHp((short)(this._pc.getCurrentHp() + drainHp));
            if (this._targetPc != null)
              this._targetPc.setCurrentHp(Math.max(this._targetPc.getCurrentHp() - drainHp, 0)); 
          } 
          if (attrWeapon.getTypeDrainMp() > 0) {
            int drainMp = 1 + _random.nextInt(attrWeapon.getTypeDrainMp());
            S_SkillSound s_SkillSound = new S_SkillSound(this._target.getId(), attrWeapon.getgfixd());
            this._target.broadcastPacketX8((ServerBasePacket)s_SkillSound);
            if (this._targetPc != null) {
              if (this._targetPc.getCurrentMp() > drainMp) {
                this._pc.setCurrentMp((short)(this._pc.getCurrentMp() + drainMp));
                this._targetPc.setCurrentMp((short)(this._targetPc.getCurrentMp() - drainMp));
              } 
            } else if (this._targetNpc != null && this._targetNpc.getCurrentMp() > drainMp) {
              this._pc.setCurrentMp((short)(this._pc.getCurrentMp() + drainMp));
              this._targetNpc.setCurrentMp((short)(this._targetNpc.getCurrentMp() - drainMp));
            } 
          } 
          if (attrWeapon.getTypeDmgup() > 0.0D) {
            S_SkillSound s_SkillSound = new S_SkillSound(this._target.getId(), attrWeapon.getgfixd());
            this._target.broadcastPacketX8((ServerBasePacket)s_SkillSound);
            reset_dmg = (int)(reset_dmg * attrWeapon.getTypeDmgup());
          } 
          if (attrWeapon.getTypeRange() > 0 && attrWeapon.getTypeRangeDmg() > 0) {
            S_SkillSound s_SkillSound = new S_SkillSound(this._target.getId(), attrWeapon.getgfixd());
            this._target.broadcastPacketX8((ServerBasePacket)s_SkillSound);
            int dmg = attrWeapon.getTypeRangeDmg() + _random.nextInt(30);
            if (this._targetPc != null) {
              Iterator<?> localIterator1 = World.get()
                .getVisibleObjects((L1Object)this._targetPc, attrWeapon.getTypeRange()).iterator();
              while (localIterator1.hasNext()) {
                L1Object tgobj = (L1Object)localIterator1.next();
                if (tgobj instanceof L1PcInstance) {
                  L1PcInstance tgpc = (L1PcInstance)tgobj;
                  if (tgpc.isDead() || (
                    tgpc.getClanid() == this._pc.getClanid() && tgpc.getClanid() != 0) || 
                    tgpc.getMap().isSafetyZone(tgpc.getLocation()))
                    continue; 
                  tgpc.receiveDamage((L1Character)this._pc, dmg, false, false);
                } 
              } 
            } else if (this._targetNpc != null) {
              Iterator<?> localIterator1 = World.get()
                .getVisibleObjects((L1Object)this._targetNpc, attrWeapon.getTypeRange()).iterator();
              while (localIterator1.hasNext()) {
                L1Object tgobj = (L1Object)localIterator1.next();
                if (tgobj instanceof L1MonsterInstance) {
                  L1MonsterInstance tgmob = (L1MonsterInstance)tgobj;
                  if (tgmob.isDead())
                    continue; 
                  tgmob.receiveDamage((L1Character)this._pc, dmg);
                } 
              } 
            } 
          } 
          if (attrWeapon.getfix_damage() > 0)
            reset_dmg += attrWeapon.getfix_damage(); 
          if (attrWeapon.getrandom_damage() > 0)
            reset_dmg += _random.nextInt(attrWeapon.getrandom_damage()); 
          if (attrWeapon.getc_hp() > 0 && this._target.getCurrentHp() > attrWeapon.getc_hp()) {
            S_SkillSound s_SkillSound = new S_SkillSound(this._target.getId(), attrWeapon.getgfixd());
            this._target.broadcastPacketX8((ServerBasePacket)s_SkillSound);
            this._pc.setCurrentHp((short)(this._pc.getCurrentHp() + attrWeapon.getc_hp()));
          } 
          if (attrWeapon.getc_mp() > 0 && this._target.getCurrentMp() > attrWeapon.getc_mp()) {
            S_SkillSound s_SkillSound = new S_SkillSound(this._target.getId(), attrWeapon.getgfixd());
            this._target.broadcastPacketX8((ServerBasePacket)s_SkillSound);
            this._pc.setCurrentMp((short)(this._pc.getCurrentMp() + attrWeapon.getc_mp()));
          } 
          if (attrWeapon.getTypeLightDmg() > 0) {
            int dmg2 = attrWeapon.getTypeLightDmg();
            this._target.broadcastPacketAll((ServerBasePacket)new S_SkillSound(this._target.getId(), attrWeapon.getgfixd()));
            if (this._targetPc != null) {
              this._targetPc.sendPackets((ServerBasePacket)new S_SkillSound(this._target.getId(), attrWeapon.getgfixd()));
              this._targetPc.receiveDamage((L1Character)this._pc, dmg2, false, false);
            } else if (this._targetNpc != null) {
              this._targetNpc.receiveDamage((L1Character)this._pc, dmg2);
            } 
          } 
          if (attrWeapon.getTypeSkill1() && attrWeapon.getTypeSkillTime() > 0.0D) {
            int timeSec = (int)attrWeapon.getTypeSkillTime();
            if (this._targetPc != null && !this._target.hasSkillEffect(40)) {
              this._targetPc.sendPacketsAll(
                  (ServerBasePacket)new S_SkillSound(this._targetPc.getId(), attrWeapon.getgfixd(), timeSec));
              this._targetPc.setSkillEffect(40, timeSec * 1000);
              this._targetPc.sendPackets((ServerBasePacket)new S_CurseBlind(1));
            } 
          } 
          if (attrWeapon.getTypeSkill2() && attrWeapon.getTypeSkillTime() > 0.0D) {
            int timeSec = (int)attrWeapon.getTypeSkillTime();
            if (this._targetPc != null && !this._target.hasSkillEffect(64)) {
              this._targetPc.sendPacketsAll(
                  (ServerBasePacket)new S_SkillSound(this._targetPc.getId(), attrWeapon.getgfixd(), timeSec));
              this._target.setSkillEffect(64, timeSec * 1000);
            } 
          } 
          if (attrWeapon.getTypeSkill3() && attrWeapon.getTypeSkillTime() > 0.0D && 
            attrWeapon.getTypePolyList() != null) {
            int polyId = Integer.parseInt(attrWeapon.getTypePolyList()[_random
                  .nextInt((attrWeapon.getTypePolyList()).length)]);
            if (this._targetPc != null) {
              if (this._targetPc.getInventory().checkEquipped(20281) || 
                this._targetPc.getInventory().checkEquipped(120281))
                return reset_dmg; 
              this._targetPc
                .sendPacketsAll((ServerBasePacket)new S_SkillSound(this._target.getId(), attrWeapon.getgfixd()));
              L1PolyMorph.doPoly((L1Character)this._targetPc, polyId, (int)attrWeapon.getTypeSkillTime(), 1);
            } else if (this._targetNpc != null && !this._targetNpc.getNpcTemplate().is_boss()) {
              this._target
                .broadcastPacketAll((ServerBasePacket)new S_SkillSound(this._target.getId(), attrWeapon.getgfixd()));
              L1PolyMorph.doPoly(this._target, polyId, (int)attrWeapon.getTypeSkillTime(), 1);
            } 
          } 
          if (attrWeapon.getTypeSkill4() && attrWeapon.getTypeSkillTime() > 0.0D) {
            int timeSec = (int)attrWeapon.getTypeSkillTime();
            if (this._targetPc != null && !this._targetPc.hasSkillEffect(87)) {
              this._targetPc.setSkillEffect(87, timeSec * 1000);
              L1SpawnUtil.spawnEffect(81162, timeSec, this._targetPc.getX(), this._targetPc.getY(), 
                  this._targetPc.getMapId(), (L1Character)this._targetPc, 0);
              this._targetPc.sendPackets((ServerBasePacket)new S_Paralysis(5, true, timeSec * 1000));
            } 
          } 
          if (attrWeapon.getTypeRemoveWeapon() && this._targetPc != null && 
            this._targetPc.getWeapon() != null) {
            this._targetPc.getInventory().setEquipped(this._targetPc.getWeapon(), false, false, false);
            this._targetPc.sendPackets((ServerBasePacket)new S_ServerMessage(1027));
          } 
          if (attrWeapon.getTypeRemoveDoll() && this._targetPc != null && 
            !this._targetPc.getDolls().isEmpty()) {
            Iterator<L1DollInstance> iter = this._targetPc.getDolls().values().iterator();
            if (iter.hasNext()) {
              L1DollInstance doll = iter.next();
              doll.deleteDoll();
            } 
          } 
          if (attrWeapon.getTypeRemoveArmor() > 0 && this._targetPc != null) {
            int counter = _random.nextInt(attrWeapon.getTypeRemoveArmor()) + 1;
            StringBuffer sbr = new StringBuffer();
            Iterator<L1ItemInstance> iterator2 = this._targetPc.getInventory().getItems().iterator();
            while (iterator2.hasNext()) {
              L1ItemInstance item = iterator2.next();
              if (item.getItem().getType2() != 2 || 
                !item.isEquipped())
                continue; 
              this._targetPc.getInventory().setEquipped(item, false, false, false);
              sbr.append("[").append(item.getNumberedName(1L)).append("]");
              if (--counter <= 0)
                break; 
            } 
            if (sbr.length() > 0) {
              this._targetPc.sendPackets((ServerBasePacket)new S_SystemMessage("以下装备被对方卸除:" + sbr.toString()));
              this._pc.sendPackets((ServerBasePacket)new S_SystemMessage("成功卸除对方以下装备:" + sbr.toString()));
            } 
          } 
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return reset_dmg;
  }
}
